#include "Position.hpp"
#include "CriticalException.hpp"
#include "Piece.hpp"

bool Position::isGood(int x, int y)
{
	return ((x > -1 && x < 8) && (y > -1 && y < 8));	
}

Position::Position(unsigned int x, unsigned int y, Chess::Color color)
	: x(x), y(y), color(color)
{
	if (color == Chess::BLACK)
	{
		x = 7 - x;
		y = 7 - y;
	}
}

Position::~Position()
{}

Position::Position(const Position& pos)
	: x(pos.x), y(pos.y), color(pos.color)
{}

void  Position::setColor(Chess::Color color)
{
	if (color != this->color)
	{
		x = 7 - x;
		y = 7 - y;
	}
	
	this->color = color;
}

unsigned int Position::getRealX() const
{
	if (color == Chess::BLACK)
		return 7 - x;
	else
		return x;
}

unsigned int Position::getRealY() const
{
	if (color == Chess::BLACK)
		return 7 - y;
	else
		return y;
}

unsigned int Position::getX() const
{	return this->x;		}

unsigned int Position::getY() const
{	return this->y;		}

void	Position::setX(unsigned int val)
{
	if (val > 7)
		throw new CriticalException("POSITION: setX : val given is greater than 7");
		
	x = val;
}

void	Position::setY(unsigned int val)
{
	if (val > 7)
		throw new CriticalException("POSITION: setY : val given is greater than 7");
		
	y = val;
}

bool 	Position::operator== (const Position& pos) const
{
	return ((this->getRealX() == pos.getRealX())
			&& (this->getRealY() == pos.getRealY()));
}

bool	Position::operator!= (const Position& pos) const
{
	return (!((this->x == pos.x)
			&& (this->y == pos.y)));
}

Position&	Position::operator= (const Position& pos)
{
	x = pos.x;
	y = pos.y;
	color = pos.color;
	
	return (*this);
}

Chess::Color	Position::getColor() const
{	return (this->color);	}
